package com.cjy.user.action;

import com.base.common.action.AbstractBaseAction;
import com.base.common.bean.ActionResult;
import com.base.common.enums.TerminalType;
import com.base.common.persistent.model.Terminal;
import com.base.common.utils.EasyUtil;
import com.cjy.common.bean.OpAttributes;
import com.cjy.user.service.UserService;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import javax.annotation.Resource;
import java.util.List;

import static com.base.GlobaConstants.*;

@Action("userAction")
@Results({ @Result(name = ASYNCHRONOUS, type = JSON, params = { ROOT,
		ACTION_RESULT_DATA, CONTENT_TYPE, CONTENT_TYPE_JSON }) })
public class UserAction extends AbstractBaseAction {

	private static final long serialVersionUID = 1L;

	private int bid = 0;	// 0,android城际云; 1,android城管家; 2,微信城际云; 3,ios城际云; 4,ios云管家
	
	private int captcha;
	
	private String code;

	private String openId;

	private String phone;
	
	private int userChooseCompoundsId;

    private String resIds;

    public String getResIds() {
        return resIds;
    }

    public void setResIds(String resIds) {
        this.resIds = resIds;
    }

    public void setBid(int bid) {
        this.bid = bid;
    }

    public void setCaptcha(int captcha) {
        this.captcha = captcha;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public void setOpenId(String openId) {
        this.openId = openId;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public void setUserChooseCompoundsId(int userChooseCompoundsId) {
        this.userChooseCompoundsId = userChooseCompoundsId;
    }

	public int getBid() {
		return bid;
	}

	public int getCaptcha() {
		return captcha;
	}

	public String getCode() {
		return code;
	}

	public String getOpenId() {
		return openId;
	}

	public String getPhone() {
		return phone;
	}

	public int getUserChooseCompoundsId() {
		return userChooseCompoundsId;
	}

    @Resource
	private UserService userService;

	/**
	 * 城际云登录
	 *
	 * @return
	 */
	public String login() {
		try {
			Terminal terminal = new Terminal();

			if(bid == 2) {
				terminal.setOpenId(openId.trim());
			} else if (bid == 3) {
				terminal.setAppleId(code.trim());
			} else {
				terminal.setCode(code.trim());
			}
			terminal.setBid(bid);
			List users = userService.login(terminal);
			session.put("users", users);
			if(users.size() > 0) {
				actionResult = ActionResult.getActionResult(new OpAttributes().add(
						"code", 1).add("result", users));
			} else {
				actionResult = ActionResult.getActionResult(new OpAttributes().add(
						"code", 0));
			}
		} catch (Exception e) {
			e.printStackTrace();
			actionResult = ActionResult.getActionResult(new OpAttributes().add(
					"code", 0));
		}
		return returnType();
	}

	/**
	 * 通知服务器准备注册
	 * 
	 * @return
	 */
	public String prepareRegiste() {
		try {
			Terminal terminal = new Terminal();
			terminal.setBid(bid);
			terminal.setUserChooseCompoundsId(userChooseCompoundsId);
			int result = 0;
			
			// 判断注册设备类型
			if(bid == 2) {
				if("-1".equals(openId.trim())){
					throw new Exception();
				}
				terminal.setOpenId(openId);
				terminal.setPhone(phone.trim());
				result = userService.weChatGetCaptcha(terminal);
			} else if(bid == 3){
				terminal.setAppleId(code);
				terminal.setPhone(phone.trim());
				result = userService.iosGetCaptcha(terminal);
			} else {
				terminal.setCode(code);
				System.out.println(phone);
				terminal.setPhone(EasyUtil.rc4(phone, "szcjy").trim());
				result = userService.androidGetCaptcha(terminal);
			}
			
			

			actionResult = ActionResult.getActionResult(new OpAttributes().add(
					"code", result));
		} catch (Exception e) {
			e.printStackTrace();
			actionResult = ActionResult.getActionResult(new OpAttributes().add(
					"code", 0));
		}
		return returnType();
	}

	/**
	 * 城际云注册
	 * 
	 * @return
	 */
	public String registe() {

		try {
			Terminal terminal = new Terminal();
			// 判断是否是哪种设备注册
			if(bid == 2) {
				terminal.setOpenId(openId);
				terminal.setPhone(phone.trim());
			} else if(bid == 3){
				terminal.setAppleId(code);
				terminal.setPhone(phone.trim());
			} else {
				terminal.setCode(code);
				terminal.setPhone(EasyUtil.rc4(phone, "szcjy").trim());
			}
			terminal.setBid(bid);
			terminal.setCaptcha(captcha);
			int code = 0;
			if(bid == 1){
				code = userService.managerRegiste(terminal);
			} else {
				code = userService.registe(terminal);
			}
			actionResult = ActionResult.getActionResult(new OpAttributes().add(
					"code", code));
		} catch (Exception e) {
			e.printStackTrace();
			actionResult = ActionResult.getActionResult(new OpAttributes().add(
					"code", 0));
		}
		return returnType();
	}

	/**
	 * 更新用户设备码
	 * 
	 * @return
	 */
	public String updateTerminalCode() {
		try {
			
		} catch (Exception e) {
			e.printStackTrace();
			actionResult = ActionResult.getActionResult(new OpAttributes().add(
					"code", 0));
		}
		return returnType();
	}

    public String updateNewUser() {
        try {
            Terminal terminal = new Terminal();
            // 判断是否是哪种设备注册
            if(bid == 2) {
                terminal.setOpenId(openId);
            } else if(bid == 3){
                terminal.setAppleId(code);
            } else {
                terminal.setCode(code);
            }
            terminal.setBid(bid);
            actionResult = ActionResult.getActionResult(new OpAttributes().add("code", 1).add("result", userService.updateNewUser(terminal)));
        } catch (Exception e) {
            e.printStackTrace();
            actionResult = ActionResult.getActionResult(new OpAttributes().add(
                    "code", 0));
        }
        return  returnType();
    }

	public String airportLogin() {
		try {
            if (bid == TerminalType.JCJY_ANDROID.getValue()) {
                phone = EasyUtil.rc4(phone, "szcjy");
            }
			actionResult = ActionResult.getActionResult(new OpAttributes().add("code", 1).add("result", userService.airportRegister(phone)));
		} catch (Exception e) {
			e.printStackTrace();
			actionResult = ActionResult.getActionResult(new OpAttributes().add(
					"code", 0));
		}
		return returnType();
	}

	public String validatePhoneNum() {
        try {
            if (bid == TerminalType.JCJY_ANDROID.getValue()) {
                phone = EasyUtil.rc4(phone, "szcjy");
            }
            actionResult = ActionResult.getActionResult(new OpAttributes().add(
                    "code", 1).add("result", userService.validatePhoneNum(phone)));
        } catch (Exception e) {
            actionResult = ActionResult.getActionResult(new OpAttributes().add(
                    "code", 0));
        }
        return returnType();
	}
}
